package com.jingdianjichi.practice.server.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import com.jingdianjichi.practice.server.Interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * ClassName:GlobalConfig
 * Package:com.jingdianjichi.subject.application.config
 * Description:
 *
 * @Author fst
 * @Create 2024/10/2 14:38
 * @Version 1.0
 */

@Configuration
public class GlobalConfig extends WebMvcConfigurationSupport
{
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(mappingJackson2HttpMessageConverter());
    }

    private  MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter()
    {
        //允许泛型为空
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);

        //不能为null
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        //将objectMapper加入
        MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter(objectMapper);

        return converter;
    }

    protected void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(new LoginInterceptor());
    }


}
